--
-- Author: shunguo.chen
-- Date: 2018-01-09 15:45:38
--
-- CosplayHeadRender cosplay 头像

local AvatarIconNode = require("app.widget.AvatarIconNode")

local ClassRef = app.mvc.createRenderClass(...)

function ClassRef.createWithData(data, node)
	local sprite = ClassRef.extend(node)
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end     
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

-- data = CosplayRole
function ClassRef:init(data)
	self.super.init(self, data)
	
	self:setContentSize(self._ui.Panel:getContentSize())

	self:setSelected(false)
	self:setChoose(false)

	self._avatarIcon = AvatarIconNode.new(self._ui.Panel, "icon")

	self._ui.Button:addClickEventListener(function()
		if not self:isSelected() then
			executeFunc(self._onClick, self)
		end
	end)

	self:refresh()
end

function ClassRef:refresh()
	if self._data then
		self._avatarIcon:update(self._data:getAvatarParams())
		self:setChoose(self._data:isChoose())

		if self._cosplayType == Enums.CosplayType.pve10 then
			self:setChoose(false)
		end
	end
end

function ClassRef:setIndex(index)
	self._index = index
end
function ClassRef:getIndex()
	return self._index
end

function ClassRef:isChoose()
	return self._ui.icon_choose:isVisible()
end

function ClassRef:setChoose(b)
	self._ui.icon_choose:setVisible(b)
	self._ui.mask:setVisible(b)
end

function ClassRef:isSelected()
	return not self._ui.Button:isBright()
end

function ClassRef:setSelected(b)
	self:setScale(b and 1 or 0.75)
	self._ui.Button:setBright(not b)
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

function ClassRef:setCurCosplayType(type)
	self._cosplayType = type
	if self._cosplayType == Enums.CosplayType.pve10 then
		self:setChoose(false)
	end
end

return ClassRef